#!/bin/bash
#==============================================================#
# File      :   copy-schema
# Desc      :   dump src schema and load it to dst
# Time      :   {{ '%Y-%m-%d %H:%M' |strftime }}
# Path      :   {{ dir_path }}/copy-schema
# Deps      :   bash
# License   :   AGPLv3 @ https://doc.pgsty.com/about/license
# Copyright :   2018-2025  Ruohang Feng / Vonng (rh@vonng.com)
#==============================================================#


#--------------------------------------------------------------#
# Utils
#--------------------------------------------------------------#
__CN='\033[0m';__CB='\033[0;30m';__CR='\033[0;31m';__CG='\033[0;32m';
__CY='\033[0;33m';__CB='\033[0;34m';__CM='\033[0;35m';__CC='\033[0;36m';__CW='\033[0;37m';
function log_info() {  printf "[${__CG} OK ${__CN}] ${__CG}$*${__CN}\n";   }
function log_warn() {  printf "[${__CY}WARN${__CN}] ${__CY}$*${__CN}\n";   }
function log_error() { printf "[${__CR}FAIL${__CN}] ${__CR}$*${__CN}\n";   }
function log_debug() { printf "[${__CB}HINT${__CN}] ${__CB}$*${__CN}\n"; }
function log_input() { printf "[${__CM} IN ${__CN}] ${__CM}$*\n=> ${__CN}"; }
function log_hint()  { printf "${__CB}$*${__CN}\n"; }
function log_line()  { printf "${__CM}[$*] ===========================================${__CN}\n"; }


#--------------------------------------------------------------#
# Param
#--------------------------------------------------------------#
# check if MIGRATION_CONTEXT is defined as expected
EXPECTED_CONTEXT="{{ src_cls }}.{{ src_db }}"
if [[ "${MIGRATION_CONTEXT}" != "${EXPECTED_CONTEXT}" ]]; then
    log_error "MIGRATION_CONTEXT = ${MIGRATION_CONTEXT} != EXPECTED ${EXPECTED_CONTEXT}"
    log_hint "did you run . activate first?"
    exit 1
fi
SQLFILE="data/schema.sql"


#--------------------------------------------------------------#
# Dump Schema
#--------------------------------------------------------------#
log_info   "dump src schema: ${SRCCLS}.${SRCDB}"
log_info   "  - SRC URL  : ${SRCPG}"
log_info   "  - SQL FILE : ${SQLFILE}"
log_debug  "pg_dump ${SRCPG} -Oscx --no-publications --no-subscriptions > ${SQLFILE}"

# WARNING: catalog / monitor / citus / timescaledb schema are excluded!
# WARNING: you have to handle timescaledb hypertable alone!
pg_dump "${SRCPG}" -Oscx --no-publications --no-subscriptions \
    -N pg_toast -N information_schema -N repack -N pg_catalog -N monitor \
    -N timescaledb_information -N timescaledb_experimental -N _timescaledb_cache -N _timescaledb_catalog -N _timescaledb_internal -N _timescaledb_config \
    -N citus -N citus_internal -N columnar -N columnar_internal -T spatial_ref_sys > ${SQLFILE}


#--------------------------------------------------------------#
# Print Schema
#--------------------------------------------------------------#
log_warn "check dumped schema before loading to dst"
log_info "grep CREATE ${SQLFILE} results: \n"
grep CREATE ${SQLFILE}


#--------------------------------------------------------------#
# Load Schema (with explicit asking)
#--------------------------------------------------------------#
log_input "load schema to dst ? (yes/no) >"
read -r
case ${REPLY} in
    [yY][eE][sS]|[yY])
        log_info   "load dst schema: ${DSTCLS}.${DSTDB}"
        log_info   "  - DST URL  : ${DSTPG}"
        log_info   "  - SQL FILE : ${SQLFILE}"
        log_debug  "$ psql '${DSTPG}' -Xwf ${SQLFILE}"
        psql "${DSTPG}" -Xwf ${SQLFILE}
        exit 0
        ;;
    *)
        log_info  "you can exam it and execute it later with:"
        log_debug  "$ psql '${DSTPG}' -Xwf ${SQLFILE}"
        exit 1
        ;;
esac
